package com.zg.shop.product;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.zg.shop.common.Constant;

@Transactional
public class ProductService {
	private ProductDao productDao;

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public List<Product> findHot() {
		List<Product> productHots = productDao.findHot();
		if(productHots != null){
			return productHots;
		}else{
			System.out.println("==================================");
			return null;
		}
		
	}

	public List<Product> findNew() {
		
		return productDao.findNew();
	}

	public PageBean<Product> findByPage(Integer cid, Integer page) {
		int totalPage = 0;
		PageBean<Product> pageBean = new PageBean<Product>();
		pageBean.setPage(page);
		pageBean.setLimit(Constant.CLIMIT);
		Integer totalCount = productDao.findCountByCid(cid);
		pageBean.setTotalCount(totalCount);
		if(totalCount%Constant.CLIMIT == 0){
			totalPage = totalCount/Constant.CLIMIT;
		}else{
			totalPage = totalCount/Constant.CLIMIT + 1;
		}
		pageBean.setTotalPage(totalPage);
		int begin = (page-1)*Constant.CLIMIT;
		List<Product> products = productDao.findByPage(cid, begin, Constant.CLIMIT);
		pageBean.setList(products);
		return pageBean;
	}

	public Product findProductInfoByPid(Integer pid) {
		Product product = productDao.findProductInfoByPid(pid);
		return product;
	}

	public PageBean<Product> findByCsId(Integer csid, Integer page) {
		int totalPage = 0;
		PageBean<Product> pageBean = new PageBean<Product>();
		pageBean.setPage(page);
		pageBean.setLimit(Constant.CSLIMIT);
		
		Integer totalCount = productDao.findCountByCsid(csid);
		pageBean.setTotalCount(totalCount);
		if(totalCount%Constant.CSLIMIT == 0){
			totalPage = totalCount/Constant.CSLIMIT;
		}else{
			totalPage = totalCount/Constant.CSLIMIT + 1;
		}
		pageBean.setTotalPage(totalPage);
		int begin = (page-1)*Constant.CSLIMIT;
		List<Product> products = productDao.findPageByCsid(csid, begin, Constant.CSLIMIT);
		pageBean.setList(products);
		return pageBean;
	}

	public PageBean findByPage(Integer page) {
		int totalPage = 0;
		PageBean<Product> pageBean = new PageBean<Product>();
		pageBean.setPage(page);
		pageBean.setLimit(Constant.CLIMIT);
		Integer totalCount = productDao.findCount();
		pageBean.setTotalCount(totalCount);
		if(totalCount%Constant.CLIMIT == 0){
			totalPage = totalCount/Constant.CLIMIT;
		}else{
			totalPage = totalCount/Constant.CLIMIT + 1;
		}
		pageBean.setTotalPage(totalPage);
		int begin = (page - 1)*Constant.CLIMIT;
		List<Product> products = productDao.findByPage(begin, Constant.CLIMIT);
		pageBean.setList(products);
		
		return pageBean;
	}

	public void save(Product product) {
		productDao.save(product);
	}
	

}
